package com.flymobi.mshop.adapter;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.flymobi.mshop.R;
import com.flymobi.mshop.activity.BuyShopping;
import com.flymobi.mshop.util.ImageUtil;
import com.flymobi.mshop.util.service.IService;
/**
 * 
 * @author 余鹏
 * 分类适配器
 */
public class ClassificationAdapter extends BaseAdapter{
	private List<Map<String, Object>> list;
	private Context context;
	
	public ClassificationAdapter() {
		// TODO Auto-generated constructor stub
	}
	public ClassificationAdapter(Context context,List<Map<String, Object>> list){
		this.context = context;
		this.list=list;
	}
	
	@Override
	public int getCount() {
		if (list == null)
			return 0;
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	private ImageView backImage;
	private TextView prices;
	private TextView market;
	private TextView shoppingName;
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		convertView = View.inflate(context, R.layout.classdata, null);
		backImage = (ImageView) convertView.findViewById(R.id.backImage);
		prices = (TextView) convertView.findViewById(R.id.prices);
		market = (TextView) convertView.findViewById(R.id.market);
		shoppingName = (TextView) convertView.findViewById(R.id.shoppingName);
		if(list!=null){
			Map<String, Object> map = list.get(position);
			final String id= map.get("id").toString();
			prices.setText("¥"+map.get("saleprice"));
			market.setText("¥"+map.get("price"));
			String imgPath=map.get("imgpath").toString();
			shoppingName.setText(map.get("name").toString());
			backImage.setImageBitmap(ImageUtil.getHttpBitmap(IService.serverUrl+imgPath));
			backImage.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent intent =new Intent();
					intent.putExtra("id",id);
					intent.setClass(context, BuyShopping.class);
					context.startActivity(intent);
				}
			});
		}
		return convertView;
	}
}
